<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/5/9
 * Time: 16:03
 */

namespace Biz\service\trade;

use Biz\model\trade\OrderRefundQueue as mOrderRefundQueue;
use Biz\module\Classes\mgt\OrderRefundQueueExcel as mgtOrderRefundQueueExcel;
/**
 * @deprecated
 */
class OrderRefundQueue
{
    public static function getOrderRefundQueueList($field, $where = [], $order = [], $limit = [])
    {

        $orderRefundWhere = self::getOrderRefundQueueWhere($where);
        $orderRefundList  = mOrderRefundQueue::getOrderRefundQueueList($field, $orderRefundWhere, $order, $limit);
        $orderRefundList  = self::formatRefundQueueList($orderRefundList);
        $orderRefundSum      = mOrderRefundQueue::getOrderRefundQueueRmbAmount($orderRefundWhere) ?: 0;

        $result = [
            'orderPayList' => $orderRefundList,
            'orderPaycount' => mOrderRefundQueue::getOrderRefundQueueCount($orderRefundWhere) ?: 0,
            'OrderPayAmount' => $orderRefundSum,
            'statusList' => mOrderRefundQueue::$orderReturnStatus,
            'payWay' => mOrderRefundQueue::$payWayDesc,
        ];
        return $result;
    }

    public static function mgtDownList($field, $data)
    {

        $orderPayWhere = self::getOrderRefundQueueWhere($data);

        mgtOrderRefundQueueExcel::downList($field, $orderPayWhere);
    }

    public static function getOrderRefundQueueWhere($where)
    {
        if ($where['payWay'] != '-1') {
            if($where['payWay'] == mOrderRefundQueue::REFUND_WAY_WXPAY_APP || $where['payWay'] == mOrderRefundQueue::REFUND_WAY_MINIPROGRAM || $where['payWay'] == mOrderRefundQueue::REFUND_WAY_TUAN_MINIPROGRAM) {
                $orderRefundQueueWhere['payWay'] = [mOrderRefundQueue::REFUND_WAY_WXPAY_APP, mOrderRefundQueue::REFUND_WAY_MINIPROGRAM,mOrderRefundQueue::REFUND_WAY_TUAN_MINIPROGRAM];
            } else {
                $orderRefundQueueWhere['payWay'] = $where['payWay'];
            }
        }

        if ($where['start_time']) {
            $orderRefundQueueWhere['start_time'] = $where['start_time'];
        }

        if ($where['end_time']) {
            $orderRefundQueueWhere['end_time'] = $where['end_time'];
        }

        if ($where['orderNo']) {
            $orderRefundQueueWhere['orderNo'] = $where['orderNo'];
        }

        if ($where['status'] != '-1'){
            $orderRefundQueueWhere['status'] = $where['status'];
        }

        if ($where['refundNo']) {
            $orderRefundQueueWhere['refundNo'] = $where['refundNo'];
        }
        return $orderRefundQueueWhere;
    }

    private static function formatRefundQueueList($orderRefundList){

        $orderRefundList = array_map(function ($orderRefundList){
            $orderRefundList['refund_rmb'] = $orderRefundList['refund_shipping_amount'] + $orderRefundList['refund_rmb'];
            return $orderRefundList;
        },$orderRefundList);

        return $orderRefundList;
    }
}